<?php

//  Author: liu21st <daigangbo@126.com>

/**
 * 后台基类
 */
class AdminAction extends Action {

    /**
     * 项目初始化
     */
    public function _initialize() {

    }
    /**
     *
     */
    public function index() {
        $Model = D($this->getActionName());
        $this->_list($Model);
        $this->display();
    }
    /**
     * 新增
     */
    public function add(){
        $this->display();
    }
    /**
     * 编辑
     */
    public function edit(){
        $Model = D($this->getActionName());
        $item  = $Model->find(intval($_GET[$Model->getPk()]));
        $this->assign('item',$item);
        $this->display();
    }
    public function update(){
       $Model = D($this->getActionName());
        if(!$Model->create()){
            $this->error($Model->getError());
        }
        if($Model->save()){
            $this->success();
        }else{
            echo M()->getLastSql();
            $this->error($Model->getError());
        }
    }
    /*
     * 添加数据到库
     */
    public function insert(){
        $Model = D($this->getActionName());
        if(!$Model->create()){
            $this->error($Model->getError());
        }
        if($Model->add()){
            $this->success();
        }else{
            $this->error($Model->getError());
        }
    }
    /**
     * 删除操作
     */
    public function delete(){
       $Model = D($this->getActionName());
     //  dump($Model);
       dump($Model->getPk());
       dump($_REQUEST[$Model->getPk()]);
       $where = array($Model->getPk()=>array('in',$_REQUEST[$Model->getPk()]));
       dump($where);
       if($Model->where($where)->delete()){
            dump($Model->getLastSql());
            $this->success('操作成功');
        }else{
            dump($Model->getLastSql());
            $this->error($Model->getError());
        }
    }
    /**
     * 数据列表
     * @param <type> $model 数据对象
     * @param <type> $map 过滤条件
     */
    protected function _list($model,$map=array()) {
        import("ORG.Util.Page");
        //创建分页对象
        $count  = $model->where($map)->count();
        if(!empty($_REQUEST['listRows'])) {
            $listRows  =  $_REQUEST['listRows'];
        }else {
            $listRows  =  18;
        }
        $Page          = new Page($count,$listRows);
        $list     = $model->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$Page->show());
        return ;
    }

}
?>